<!DOCTYPE html>
<html>
    <head>
        <title>MediaKeySession lifetime after release()</title>
        <script src="encrypted-media-utils.js"></script>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <script>
            // MediaKeySessions remain as long as:
            //   JavaScript has a reference to it
            //   OR (MediaKeys is around
            //       AND the session has not received a close() event)
            promise_test(function(test)
            {
                var mediaKeys;
                var mediaKeySession1;
                var mediaKeySession2;
                var initDataType;
                var initData;

                // Create 2 sessions.
                // Even though there should be no existing objects, start by
                // running gc() and verifying that none exist. This also
                // allows the failures to be reported from inside a promise
                // so that the test fails properly.
                return createGCPromise().then(function() {
                    verifyMediaKeyAndMediaKeySessionCount(0, 0, 'After initial gc()');

                    return navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfiguration());
                }).then(function(access) {
                    initDataType = access.getConfiguration().initDataTypes[0];
                    initData = getInitData(initDataType);
                    return access.createMediaKeys();
                }).then(function(result) {
                    mediaKeys = result;
                    verifyMediaKeyAndMediaKeySessionCount(1, 0, 'MediaKeys.create()');

                    mediaKeySession1 = mediaKeys.createSession();
                    return mediaKeySession1.generateRequest(initDataType, initData);
                }).then(function() {
                    verifyMediaKeyAndMediaKeySessionCount(1, 1, 'MediaKeys.createSession(1)');

                    mediaKeySession2 = mediaKeys.createSession();
                    return mediaKeySession2.generateRequest(initDataType, initData);
                }).then(function() {
                    verifyMediaKeyAndMediaKeySessionCount(1, 2, 'mediaKeys.createSession(2)');

                    // Close the sessions. Once completed, only the JS
                    // reference to them keeps them around.
                    return mediaKeySession1.close();
                }).then(function(result) {
                    return mediaKeySession2.close();
                }).then(function(result) {
                    // Since both sessions have been closed, dropping the
                    // reference to them from JS will result in the session
                    // being garbage-collected.
                    verifyMediaKeyAndMediaKeySessionCount(1, 2, 'after close');

                    mediaKeySession1 = null;
                    return createGCPromise();
                }).then(function() {
                    verifyMediaKeyAndMediaKeySessionCount(1, 1, 'mediaKeySession1 not collected');

                    mediaKeySession2 = null;
                    return createGCPromise();
                }).then(function() {
                    verifyMediaKeyAndMediaKeySessionCount(1, 0, 'mediaKeySession2 not collected');
                });
            }, 'MediaKeySession lifetime after release()');
        </script>
    </body>
</html>
